import cv2
import numpy as np

def detect(img):
    # the color threshold (red)
    lower_range = np.array([0, 0, 150], dtype="uint8")
    upper_range = np.array([50, 50, 255], dtype="uint8")

    img = cv2.inRange(img, lower_range, upper_range)
    cv2.imshow("Range", img)

    m = cv2.moments(img)
    if m["m00"] != 0:
        x = int(m["m10"] / m["m00"])
        y = int(m["m01"] / m["m00"])
    else:
        x = 0
        y = 0
    return x, y


cam = cv2.VideoCapture(0)

last_x = 0
last_y = 0

while cam.isOpened():
    ret, frame = cam.read()

    cur_x, cur_y = detect(frame)

    # Draw the green line
    if cur_x != 0 and cur_y != 0:
        cv2.line(frame, (cur_x, cur_y), (last_x, last_y), (0, 255, 0), 10)
        last_x = cur_x
        last_y = cur_y
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('Q'):
        break

cam.release()
cv2.destroyAllWindows()
